import java.awt.Color ;

public class PlotActiveVsTime {
	static final int NUM_NEURONS = 40 ;
	static final int NUM_BIASED = 10 ;
	
	static final int T_START = 0 ;
	static final int T_STOP = 7000 ;
	
	// how many time steps can a neuron go without firing and still be considered active
	static final int ACTIVE_TIME_ZONE = 100 ;
		
	// how many runs to do for each curve
	static final int AVG_OVER_RUNS = 20 ;
	
	
	public static void addCurveForPercentSynapses ( Graph g , final double percent ,
	                                                final INeuralNetExperiment e , Color c ) {
		int i , j ;
		double [ ] y , t , temp ;
		ExperimentAnalyzer a ;
		int width = T_STOP - T_START ;
		Curve curve ;
		
		y = new double [ width ] ;
		t = new double [ width ] ;
		for ( i = 0 ; i < width ; i ++ ) {
			t [ i ] = T_START + i ;
			y [ i ] = 0 ;
		}
		
		e.setParam ( "initial syn %" , "" + percent ) ;
		
		for ( i = 0 ; i < AVG_OVER_RUNS ; i ++ ) {
			e.run ( ) ;
			a = new ExperimentAnalyzer ( e ) ;
			temp = a.numActiveBetween ( T_START , T_STOP , ACTIVE_TIME_ZONE ) ;
			for ( j = 0 ; j < width ; j ++ ) {
				y [ j ] += temp [ j ] ;
			}
		}
		
		for ( j = 0 ; j < width ; j ++ ) {
			y [ j ] /= AVG_OVER_RUNS ;
		}
		
		try {
			curve = new Curve ( t , y , "" + percent ) ;
			curve.setColor ( c ) ;
			curve.isVisible = true ;
		
			g.add ( curve ) ;
			g.autoWindow ( ) ;
			g.autoLabel ( ) ;						
		} catch ( Exception ex ) {
		}
	}
	
	public static void main ( String [ ] args ) {
		final INeuralNetExperiment e = new Experiment2 ( ) ;
		final INeuron neuron = new MapNeuron ( ) ;
		//final ISynapse synapse = new PureHebbianPlasticity ( null , null , 0. ) ;
		final ISynapse synapse = new STDPSynapse ( null , null , 0. ) ;

		// sinusoidal current with amp=.5, period = 50 until time=4000
		final IAppliedCurrent iApp = new SinusoidalCurrent ( .5 , 50 , 0 , 0 , 4000 ) ;
		
		Graph g = new Graph ( ) ;
		
		e.setCurrent ( iApp ) ;
		e.setParam ( "numNeurons" , "" + NUM_NEURONS ) ;
		e.setParam ( "numBiased" , "" + NUM_BIASED ) ;
		e.setParam ( "runTime" , "" + T_STOP ) ;
		e.setParam ( "gridSize" , "40" ) ;
		e.setParam ( "synapse variance" , ".005" ) ;
		// k controls generation of new synapses... we don't do that except at the
		// beginning so no difference (except in run time) as long as k < size_neuron
		e.setParam ( "k" , ".5" ) ;
		
		e.init ( neuron , synapse ) ;
		
		g.makeWindow ( 640 , 480 , Graph.QUIT_ON_CLOSE , "Active Neurons vs Time" ) ;
		
		addCurveForPercentSynapses ( g , 10 , e , Color.RED ) ;
		addCurveForPercentSynapses ( g , 30 , e , Color.BLUE ) ;
		addCurveForPercentSynapses ( g , 50 , e , Color.GREEN ) ;
		addCurveForPercentSynapses ( g , 70 , e , Color.ORANGE ) ;
		addCurveForPercentSynapses ( g , 90 , e , Color.BLACK ) ;
	}
}
